import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routers'

Vue.use(VueRouter)

const router = new VueRouter({
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  }
})
router.beforeEach((to, from, next) => {
  /*路由发生改变修改页面的title */
  if (to.meta?.title) {
    document.title = to.meta.title || ''
  }
  if (!to.matched.length) {
    next('/err')
    return
  }
  next()
})
export default router
